package com.huoyun.cargo.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.huoyun.base.action.BaseAction;
import com.huoyun.base.bean.Page;
import com.huoyun.car.bean.Car;
import com.huoyun.car.bean.DriverSearcher;
import com.huoyun.car.service.CarService;
import com.huoyun.car.service.DriverSearcherService;
import com.huoyun.cargo.service.CargoService;
import com.huoyun.cargo.bean.CargoInfo;
import com.huoyun.cargo.bean.CargoItem;
import com.huoyun.common.bean.BidInfo;
import com.huoyun.common.service.BidInfoService;

public class CargoSelectionAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String startProvince;

	private String startCity;

	private String startDistrict;

	private String destinationProvince;

	private String destinationCity;

	private String destinationDistrict;

	private String loadStart;

	private String loadEnd;
	
	private String bid;
	
	private int goodsId;
	
	private int priceMethod;
	
	private float weight;
	
	private DriverSearcher driverSearcher;
	
	private CarService carService;
	private CargoService cargoService;
	private DriverSearcherService driverSearcherService;
	private List<CargoItem> cargoItemList;
	private List<Car> carList;
	
	private BidInfoService bidInfoService;

	private int carDetailID;
	
	private Date loadTime;
	
	private CargoInfo cargoInfo;
	
	/**
	 * 货物最晚装载时间
	 * 车主应标时选择"到达发货地时间"的最晚时间
	 */
	private String loadEndTimeStr;
	/**
	 * 货物最早装载时间
	 * 车主应标时选择"到达发货地时间"的最早时间
	 */
	private String loadStartTimeStr;
	
	/**
	 * 根据条件搜索出的一页货源信息
	 */
	private Page cargoItemPage;
	
	//基础查询功能
	public String queryCargoBasic(){
		
		if (driverSearcher.getCargoType().compareTo(", ")==0)
		{
			driverSearcher.setCargoType(null);
		}
		
		driverSearcher.setUserId(getLoginUser().getId());
		
		if(driverSearcherService.getDriverSearcherByUserId(getLoginUser().getId()) == null) {
			driverSearcherService.saveDriverSearcher(driverSearcher);
		}else{
			driverSearcherService.updateDriverSearcherByUserId(driverSearcher);
		}
		
		com.huoyun.cargo.service.evt.SelectCargosForCarEvt searchCriteria = new com.huoyun.cargo.service.evt.SelectCargosForCarEvt();
		
		searchCriteria.copyData(driverSearcher);
		
		searchCriteria.setPageNo(pageNo);
		searchCriteria.setPageCount(PAGE_COUNT);
		
		cargoItemPage = cargoService.selectCargosForCar(searchCriteria);
		
		return SUCCESS;
	}
	
	public String adminPreSearchGoods(){
		//CargoUserInfo cargoUser=new CargoUserInfo();
		//cargoUserInfoList=cargoService.getCargoUserByCondition(cargoUser);
		return SUCCESS;
	}
	
	public String preSearchGoods() {
		
		this.driverSearcher = this.driverSearcherService.getDriverSearcherByUserId(getLoginUser().getId());
		
		if(driverSearcher == null) {
			cargoItemList = null;
		}else{		
			com.huoyun.cargo.service.evt.SelectCargosForCarEvt searchCriteria = new com.huoyun.cargo.service.evt.SelectCargosForCarEvt();
			searchCriteria.setStartProvince(driverSearcher.getFromProvince());
			searchCriteria.setStartCity(driverSearcher.getFromCity());
			searchCriteria.setStartTown(driverSearcher.getFromTown());
			searchCriteria.setDestinationProvince(driverSearcher.getToProvince());
			searchCriteria.setDestinationCity(driverSearcher.getToCity());
			searchCriteria.setDestinationTown(driverSearcher.getToTown());
			
			searchCriteria.setPageNo(pageNo);
			searchCriteria.setPageCount(PAGE_COUNT);
			cargoItemPage = cargoService.selectCargosForCar(searchCriteria);
		}
		
		return SUCCESS;
	}
	
	public String getCar2Bid() {
		
		this.goodsId = Integer.parseInt(getRequest().getParameter("goodsId"));
		bid = getRequest().getParameter("bid");
		cargoInfo = cargoService.getCargoInfoById(goodsId);
		
		//可选作应标车辆的车牌号
		carList = carService.getCarListForCargoByAmount(getLoginUser().getId(), cargoInfo.getAmount(), this.goodsId);
		
		//到达装货地点的最早、最晚时间
		loadEndTimeStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(cargoInfo.getLoadEndTime());
		loadStartTimeStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(new Date());
		
		return SUCCESS;
	}

	public String submitBid() {
		
		BidInfo bidInfo = new BidInfo();
		bidInfo.setGoodsId(cargoInfo.getId());
		bidInfo.setCarId(carDetailID);
		bidInfo.setStatus(0);
		bidInfo.setPrice(bid);
		bidInfo.setLoadTime(loadTime);		
		
		bidInfoService.saveBidInfo(bidInfo);
		
		return SUCCESS;
	}
	
	public int getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}

	public int getPriceMethod() {
		return priceMethod;
	}

	public void setPriceMethod(int priceMethod) {
		this.priceMethod = priceMethod;
	}	

	public List<CargoItem> getCargoItemList() {
		return cargoItemList;
	}

	public void setCargoItemList(List<CargoItem> cargoItemList) {
		this.cargoItemList = cargoItemList;
	}

	public String getStartProvince() {
		return startProvince;
	}

	public void setStartProvince(String startProvince) {
		this.startProvince = startProvince;
	}

	public String getStartCity() {
		return startCity;
	}

	public void setStartCity(String startCity) {
		this.startCity = startCity;
	}

	public String getStartDistrict() {
		return startDistrict;
	}

	public void setStartDistrict(String startDistrict) {
		this.startDistrict = startDistrict;
	}

	public String getDestinationProvince() {
		return destinationProvince;
	}

	public void setDestinationProvince(String destinationProvince) {
		this.destinationProvince = destinationProvince;
	}

	public String getDestinationCity() {
		return destinationCity;
	}

	public void setDestinationCity(String destinationCity) {
		this.destinationCity = destinationCity;
	}

	public String getDestinationDistrict() {
		return destinationDistrict;
	}

	public void setDestinationDistrict(String destinationDistrict) {
		this.destinationDistrict = destinationDistrict;
	}


	public String getLoadStart() {
		return loadStart;
	}

	public void setLoadStart(String loadStart) {
		this.loadStart = loadStart;
	}

	public String getLoadEnd() {
		return loadEnd;
	}

	public void setLoadEnd(String loadEnd) {
		this.loadEnd = loadEnd;
	}

	public DriverSearcher getDriverSearcher() {
		return driverSearcher;
	}

	public void setDriverSearcher(DriverSearcher driverSearcher) {
		this.driverSearcher = driverSearcher;
	}

	public DriverSearcherService getDriverSearcherService() {
		return driverSearcherService;
	}

	public void setDriverSearcherService(DriverSearcherService driverSearcherService) {
		this.driverSearcherService = driverSearcherService;
	}

	public CargoService getCargoService() {
		return cargoService;
	}

	public void setCargoService(CargoService cargoService) {
		this.cargoService = cargoService;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}

	public List<Car> getCarList() {
		return carList;
	}

	public void setCarList(List<Car> carList) {
		this.carList = carList;
	}

	public int getCarDetailID() {
		return carDetailID;
	}

	public void setCarDetailID(int carDetailID) {
		this.carDetailID = carDetailID;
	}
	public float getWeight() {
		return weight;
	}

	public void setWeight(float weight) {
		this.weight = weight;
	}
	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public BidInfoService getBidInfoService() {
		return bidInfoService;
	}

	public void setBidInfoService(BidInfoService bidInfoService) {
		this.bidInfoService = bidInfoService;
	}

	public Date getLoadTime() {
		return loadTime;
	}

	public void setLoadTime(Date loadTime) {
		this.loadTime = loadTime;
	}

	public CargoInfo getCargoInfo() {
		return cargoInfo;
	}

	public void setCargoInfo(CargoInfo cargoInfo) {
		this.cargoInfo = cargoInfo;
	}

	public String getLoadEndTimeStr() {
		return loadEndTimeStr;
	}

	public void setLoadEndTimeStr(String loadEndTimeStr) {
		this.loadEndTimeStr = loadEndTimeStr;
	}

	public String getLoadStartTimeStr() {
		return loadStartTimeStr;
	}

	public void setLoadStartTimeStr(String loadStartTimeStr) {
		this.loadStartTimeStr = loadStartTimeStr;
	}

	public Page getCargoItemPage() {
		return cargoItemPage;
	}

	public void setCargoItemPage(Page cargoItemPage) {
		this.cargoItemPage = cargoItemPage;
	}
}
